/**
 * 
 */
package graphique;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import piece.Cavalier;
import piece.Fou;
import piece.Piece;
import piece.PieceType;
import piece.Pion;
import piece.Reine;
import piece.Tour;
import core.Couleur;
import core.MutationException;

/**
 * @author baptiste.andrini
 *
 */
public class JFramePromotion extends JFrame implements MouseListener,
		MouseMotionListener {

	private static final long serialVersionUID = 1L;

	Piece pionPromu;
	
	JPanel chessBoard;
	JLayeredPane jlp;
	JLabelPiece chessPiece;
	JLabelPiece pionQuiMute;
	Fenetre_Echiquier parent;
	
	public JFramePromotion(Couleur	coul, Piece pionPromu, Fenetre_Echiquier parent, JLabelPiece pionQuiMute){
		this.pionPromu = pionPromu;
		this.parent = parent;
		this.pionQuiMute = pionQuiMute;
		
		this.setTitle("Joueur "+coul.toString()+", choisissez la Promotion de votre Pion : ");
		this.setSize(500, 150);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);

		jlp = new JLayeredPane();
		getContentPane().add(jlp);
		
		jlp.addMouseListener(this);
		jlp.addMouseMotionListener(this);
		
		chessBoard = new JPanel();
		Dimension boardSize = new Dimension(500, 150);
		chessBoard.setPreferredSize(boardSize);
		chessBoard.setBounds(0,0,boardSize.width,boardSize.height);
		
		jlp.add(chessBoard,JLayeredPane.DEFAULT_LAYER);
		chessBoard.setLayout(new GridLayout(1, 4));
		
		File f=new File("");
		String rp = f.getAbsolutePath()+"/src/graphique/icon_piece";
		
		for(PieceType pt : PieceType.getTypesMutables()){
			JPanel square = new JPanel(new BorderLayout());
			rp = f.getAbsolutePath()+"/src/graphique/icon_piece";
			rp = rp+"/"+coul.toString()+"/"+pt.toString()+".png";
			chessBoard.add(square);
			
			System.out.println(rp);
			
			if(pt == PieceType.Reine){
				JLabelPiece jla = new JLabelPiece(new ImageIcon(rp), new Reine());
				square.add(jla);
				System.out.println("reine");
			}else if(pt == PieceType.Cavalier){
				JLabelPiece jla = new JLabelPiece(new ImageIcon(rp), new Cavalier());
				square.add(jla);
				System.out.println(" cavaier");
			}else if(pt == PieceType.Fou){
				JLabelPiece jla = new JLabelPiece(new ImageIcon(rp), new Fou());
				square.add(jla);
				System.out.println("fou");
			}else if(pt == PieceType.Tour){
				JLabelPiece jla = new JLabelPiece(new ImageIcon(rp), new Tour());
				square.add(jla);
				System.out.println("tour");
			}
		}
		
		System.out.println("End promo.");
		this.setVisible(true);
	}
	
	
	/* (non-Javadoc)
	 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent arg0) {
		chessPiece = null;
		Component c =  chessBoard.findComponentAt(arg0.getX(), arg0.getY());
		
		if(c instanceof JPanelCase){
			return;
		}

		chessPiece = (JLabelPiece)c;
		try {
			System.out.println("essai mutation en : "+chessPiece.getPiece().getType());
			pionPromu = ((Pion)pionPromu).mute(chessPiece.getPiece().getType());
			parent.repeindrePiecePromu(pionPromu,pionQuiMute);
			this.dispose();
		} catch (MutationException e) {
		}
		System.out.println("APres mutation : "+pionPromu.getType());
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
